//
//  EvaluateView.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/13.
//  Copyright © 2016年 吴可高. All rights reserved.
//

#import "EvaluateView.h"
#import "ProductHeaderView.h"

@interface EvaluateView ()<UITableViewDataSource,UITableViewDelegate,ProductHeaderDelegate>
@property (strong, nonatomic) UITableView * evaluateTableView;
@property (strong, nonatomic) ProductHeaderView * productHeaderView;

@end
void * kProductContentViewKey = &kProductContentViewKey;
@implementation EvaluateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}
#pragma mark Initialize View Style
-(void)setUp
{
    __weak __typeof(self)weakSelf = self;
     self.backgroundColor = [UIColor redColor];
    [self addSubview:self.evaluateTableView];
    [self.evaluateTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
        make.edges.mas_equalTo(weakSelf).with.insets(insets);
    }];
    self.evaluateTableView.tableHeaderView = self.productHeaderView;

    [self.productHeaderView addObserver:self forKeyPath:@"productHeaderHeight" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:kProductContentViewKey];
}

#pragma mark View Algorithm Processor
#pragma mark Setter Method View Style
#pragma mark Getter Method View Style
-(UITableView *)evaluateTableView
{
    _evaluateTableView  = ({
        if (!_evaluateTableView) {
            _evaluateTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
            _evaluateTableView.delegate = self;
            _evaluateTableView.dataSource= self;
        }
        _evaluateTableView;
    });
    return _evaluateTableView;
}

-(ProductHeaderView *)productHeaderView
{
    _productHeaderView= ({
        if (!_productHeaderView) {
            _productHeaderView = [[ProductHeaderView alloc]init];
            _productHeaderView.frame  = CGRectMake(2, 2, kScreen_Width- 4, kScreen_Height *.2);
        }
        _productHeaderView.delegate = self;
        _productHeaderView;
    });
    return _productHeaderView;
}

#pragma mark IBOutlet Action View
#pragma mark Possess  Delegate View（委托代理者）

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == kProductContentViewKey) {
        
        CGFloat  changedValue = [change[@"new"]floatValue];
        self.evaluateTableView.tableHeaderView = nil;
        CGRect frame = CGRectMake(0, 0, CGRectGetWidth(self.productHeaderView.frame), changedValue);
        self.productHeaderView.frame = frame;
        self.evaluateTableView.tableHeaderView = self.productHeaderView;
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuseIndefiner = @"resueIndefiner";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIndefiner];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIndefiner];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"evaluate_class_id_%zd",indexPath.row];
    return cell;
}
#pragma mark Other Method
-(void)layoutSubviews
{
    [super layoutSubviews];
}
-(void)dealloc
{
    [self.productHeaderView  removeObserver:self forKeyPath:@"productHeaderHeight" context:kProductContentViewKey];
}
@end
